<template>
  <a-card>
    <template #title>
      <a-form layout="vertical">
        <a-row :gutter="24">
          <a-col :md="6" :xs="24">
            <a-form-item :label="$t('交易流水')">
              <a-input v-model:value="page.tradeNo"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :xs="24">
            <a-form-item :label="$t('交易类型渠道')">
              <a-select v-model:value="page.type" allowClear>
                <a-select-option :value="1">{{ $t('STP打款') }}</a-select-option>
                <a-select-option :value="2">{{ $t('STP回款') }}</a-select-option>
                <a-select-option :value="3">{{ $t('OXXO回款') }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :xs="24">
            <a-form-item :label="$t('状态')">
              <a-select v-model:value="page.status" allowClear>
                <a-select-option :value="1">{{ $t('成功') }}</a-select-option>
                <a-select-option :value="0">{{ $t('失败') }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :xs="24">
            <a-form-item :label="$t('创建时间')">
              <a-range-picker style="width: 100%" v-model:value="page.createTime" valueFormat="YYYY-MM-DD"
                              format="YYYY-MM-DD"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :xs="24">
            <a-form-item :label="$t('完成时间')">
              <a-range-picker style="width: 100%" v-model:value="page.resolveTime" valueFormat="YYYY-MM-DD"
                              format="YYYY-MM-DD"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24" style="text-align: right">
            <a-button type="primary" @click="getTableData">{{ $t('查询') }}</a-button>
            <a-button style="margin-left: 10px" @click="resetSearch">{{ $t('重置') }}</a-button>
          </a-col>
        </a-row>
      </a-form>
    </template>
    <div style="margin-bottom: 10px;text-align: right">
      <a-button type="primary" style="margin-left: 10px" @click="batchPush" v-permiss="['batch-push']">{{
          $t('批量推送')
        }}
      </a-button>
    </div>
    <a-table row-key="id" :data-source="tableData" :columns="tableColumn" :row-selection="rowSelection"
             :scroll="{x:1000}"
             :pagination="pagination">
      <template #handle="{record}">
        <a-button v-if="record.status === 0" type="link" @click="push(record)" v-permiss="['push']">{{
            $t('推送')
          }}
        </a-button>
      </template>
    </a-table>
  </a-card>
</template>

<script>
import {onMounted, reactive, ref, toRefs, computed} from 'vue'
import {fetchNoticeList, batchNoticePush, noticePush} from "../../../api/notice";
import {message, Modal} from "ant-design-vue";
import i18n from "../../../locale";
const $t = i18n.global.t
const tableColumn = [
  {title: $t('交易流水'), dataIndex: 'tradeNo', width: 150, align: 'center'},
  {
    title: $t('交易类型渠道'), dataIndex: 'type', width: 150, align: 'center', customRender: ({text}) => {
      return text === 1 ? $t('STP打款') : text === 2 ? $t('STP回款') : $t('OXXO回款')
    }
  },
  {title: $t('交易金额'), dataIndex: 'amount', width: 100, align: 'center'},
  {title: $t('创建时间'), dataIndex: 'createTime', width: 150, align: 'center'},
  {title: $t('完成时间'), dataIndex: 'bankNo', width: 150, align: 'center'},
  {title: $t('通知次数'), dataIndex: 'noticeNum', width: 100, align: 'center'},
  {
    title: $t('状态'), dataIndex: 'status', width: 100, align: 'center', customRender: ({text}) => {
      return text === 1 ? $t('成功') : $t('失败')
    }
  },
  {title: $t('操作'), width: 100, fixed: 'right', align: 'center', slots: {customRender: 'handle'}},
]
export default {
  name: "NoticeList",
  setup() {
    const state = reactive({
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      tableData: [],
      merchantList: [],
      statusForm: {},
      exportLoading: false,
      selectedRowKeys: []
    })
    const pagination = computed(() => ({
      current: state.page.pageNum,
      total: state.page.total,
      pageSize: state.page.pageSize,
      showSizeChanger: true,
      showQuickJumper: true,
      pageSizeOptions: ['10', '50', '100', '200'],
      showTotal: total => total + $t('条'),
      onChange: (value) => {
        state.page.pageNum = value
        methods.getTableData()
      },
      onShowSizeChange: (page, size) => {
        state.page.pageNum = page
        state.page.pageSize = size
        methods.getTableData()
      }
    }))
    const rowSelection = computed(() => ({
      selectedRowKeys: state.selectedRowKeys,
      onChange: (value) => {
        state.selectedRowKeys = value
      }
    }))
    const methods = {
      async getTableData() {
        const [createTimeStart, createTimeEnd] = state.page.createTime || []
        const [endTimeStart, endTimeEnd] = state.page.resolveTime || []
        const params = {
          ...state.page,
          createTimeStart, createTimeEnd, endTimeStart, endTimeEnd,
          createTime: undefined,
          resolveTime: undefined
        }
        const res = await fetchNoticeList(params)
        if (res.code === 200) {
          const {list, total} = res.data
          state.tableData = list
          state.page.total = total
        }
      },
      resetSearch() {
        state.page = {
          pageNum: 1,
          pageSize: 10,
          total: 0
        }
        methods.getTableData()
      },
      batchPush() {
        if (state.selectedRowKeys.length <= 0) return message.error($t('请勾选订单'))
        Modal.confirm({
          title: $t('确认推送') + '?',
          async onOk() {
            const res = await batchNoticePush(state.selectedRowKeys)
            if (res.code === 200) {
              message.success($t('操作成功'))
              methods.getTableData()
              state.selectedRowKeys = []
            }
          },
        });
      },
      push({id}) {
        Modal.confirm({
          title: $t('确认推送') + '?',
          async onOk() {
            const res = await noticePush({id})
            if (res.code === 200) {
              message.success($t('操作成功'))
              methods.getTableData()
            }
          },
        });
      },
    }
    onMounted(() => {
      methods.getTableData()
    })
    return {
      pagination,
      rowSelection,
      tableColumn,
      ...methods,
      ...toRefs(state)
    }
  }
}
</script>

<style scoped>

</style>
